package com.sntk.organauthomanager.infrastructure.dao;

import com.sntk.organauthomanager.domain.model.User;
import com.sntk.organauthomanager.domain.repository.UserRepository;
import com.sntk.organauthomanager.infrastructure.convert.UserConvert;
import com.sntk.organauthomanager.infrastructure.db.entity.UserEntity;
import com.sntk.organauthomanager.infrastructure.db.repository.UserDBRepository;
import com.sntk.organauthomanager.infrastructure.exception.ObjectExistedException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @ClassName: UserRepositoryImpl
 * @author: wgchao
 * @createTime: 2020/7/21 8:41 AM
 * @Description: TODO
 */
@Component
public class UserRepositoryImpl implements UserRepository {
    @Autowired
    private UserDBRepository userDBRepository;
    @Autowired
    private UserConvert userConvert;

    @Override
    public String encodePassword(String password) {

        return new BCryptPasswordEncoder().encode(password);

    }

    @Override
    public boolean matchPassword(String password, String encodePwd) {
        return new BCryptPasswordEncoder().matches(password, encodePwd);
    }

    @Override
    public void add(User user, String operator) {
        UserEntity userEntity = userDBRepository.findFirstByUnameAndDelFalse(user.getUname());
        if(user.isAdd()){
            if(userEntity != null){
                throw new ObjectExistedException("账户号已被使用");
            }
            userEntity = userConvert.convert(user);
            userEntity.setDel(false);
            userEntity.setCreator(operator);
            userEntity.setCreateTime(new Date());
            userEntity.setLastUpdateTime(new Date());
            userEntity.setLastUpdator(operator);
            userEntity = userDBRepository.save(userEntity);
        }else{
            if(userEntity == null){
                throw new ObjectExistedException("账户不存在");
            }
            UserEntity userEntityNew = userConvert.convert(user);
            userEntityNew.setDel(userEntity.getDel());
            userEntityNew.setCreator(userEntity.getCreator());
            userEntityNew.setCreateTime(userEntity.getCreateTime());
            userEntityNew.setLastUpdateTime(new Date());
            userEntityNew.setLastUpdator(operator);
            userEntityNew = userDBRepository.save(userEntityNew);
        }
    }

    @Override
    public User findByKey(String s) {
        UserEntity userEntity = userDBRepository.findFirstByUnameAndDelFalse(s);
        if(userEntity == null){
            return null;
        }
        return userConvert.convert(userEntity);
    }

    @Override
    public void delete(String s, String operator) {
        UserEntity userEntity = userDBRepository.findFirstByUnameAndDelFalse(s);
        if(userEntity == null){
            throw new ObjectExistedException("账户不存在");
        }
        userEntity.setLastUpdator(operator);
        userEntity.setLastUpdateTime(new Date());
        userEntity.setDel(true);
        userEntity = userDBRepository.save(userEntity);
    }

    public static void main(String[] args){
        System.out.println(new BCryptPasswordEncoder().encode("123456"));
    }
}
